#include <stdint.h>
#include <sys/types.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <stdio.h>
#include <pthread.h>
#include <poll.h>

static void* calls(void *ptr) {
	int ret;
	char *buf = (char*) calloc(1024, sizeof(char));
	int fd = open("/dev/t01", O_RDWR, O_NONBLOCK);
	if (fd < 0) {
		perror("open");
		printf("error open\n");
		exit(EXIT_FAILURE);
	}

	struct pollfd fds[1];
	fds[0].fd = fd;
	fds[0].events = POLLIN;
	fds[0].revents = 0;
	while (1) {
		printf("start poll\n");
		ret = poll(fds, 1, -1);
		if (ret == -1) {
			perror("poll test");
			return NULL;
		}
		if (fds[0].revents & POLLIN) {
			ret = read(fds[0].fd, buf, 1024);
			if (ret < 0) {
				perror("read test");
				return NULL;
			}
			buf[ret] = '\0';
			printf("read: %s\n", buf);
		}
		sleep(1);
	}
	close(fd);
	return NULL;
}

int main() {
	pthread_t thread;
	pthread_create(&thread, NULL, calls, NULL);
	pthread_join(thread, NULL);
	printf("main exit\n");
	return EXIT_SUCCESS;
}

